package com.lanou.sfu.songforu.JPush;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.WelcomeActivity;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by dllo on 15/11/3.
 */
public class MyNetWorkBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {

            Bundle bundle = intent.getExtras();
            String extra = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            //收到自定义消息 需要自己显示在通知栏中
            showNotification(context, extra);
        }
    }
    public void showNotification(Context context, String msg) {
        // 显获得
        NotificationManager manager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setTicker(msg);
        builder.setContentTitle(msg);
        builder.setContentText(msg);
        builder.setWhen(System.currentTimeMillis());

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                R.mipmap.icon_mv_play_count);

        builder.setLargeIcon(bitmap);// 设置大图
        builder.setAutoCancel(true);
        builder.setOngoing(true);
        builder.setSmallIcon(R.mipmap.icon);
        // 配置点击消息之后的跳转
        Intent intent = new Intent(context, WelcomeActivity.class);
        PendingIntent pi = PendingIntent.getActivity(context, 100, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pi);
        // 发送系统通知
        manager.notify(1, builder.build());
    }
}
